<?php
/*
    Theory of operation.

    Texts come from database queries.
    Concatenate them into a temp file stored
    in transient/download_tmp/projectid_modifier.txt
*/

$relPath="./../../pinc/";
require_once $relPath."dpinit.php";
include_once($relPath.'dp_main.inc');

$User->IsLoggedIn()
    or RedirectToLogin();

$projectid          = ArgProjectId();
$roundid            = Arg("roundid");
$include_proofers   = ArgBoolean("include_proofers"); 
$onefile            = ArgBoolean("onefile");
$which_text         = Arg("which_text");

// only people who can see names on the page details page
// can see names here.
$project = new DpProject($projectid);

if ($include_proofers && ! $project->UserMaySeeNames())
{
    $include_proofers = ($include_proofers 
                        && $project->UserMaySeeNames());
}

echo "projectid        = '$projectid'<br>\n";
echo "roundid          = '$roundid'<br>\n";
echo "which_text       = '$which_text'<br>\n";
echo "include_proofers = '$include_proofers'<br>\n";


$filestub = $projectid
    . ($roundid == "OCR"
        ? "_OCR"
        : $roundid)
    . ($which_text == "EQ"
        ? "_saved"
        : '_latest');

if($roundid == "OCR")
{
    $rows = $project->PageOCRText();
}
else 
{
    if($which_text == "EQ")
    {
        $rows = $project->PageRoundTextArray($roundid);
    }
    else
    {
        $rows = $project->PageActiveTextArray($roundid);
    }
}

// join the page texts together

if($onefile)
{
    $text = "";
    foreach($rows as $page)
    {
        $hdrrow = sprintf("-----File: %s---", $page['pagename']);
        if($include_proofers && isset($page['proofers']))
        {
            $hdrrow .= $page['proofers'];
        }
        $hdrrow = str_pad($hdrrow, 74, "-", STR_PAD_RIGHT);
        $text .= "$hdrrow\n";
        $text .= ($page['text'] == ""
                    ? _("not proofed")
                    : $page['text'])
            ."\n";
    }
    $text = preg_replace("/[\r\n]+/su", "\n", $text);

// make sure that everything will be cleaned up on exit
    // register_shutdown_function("clean_up_temp", $textfile_path);

    // say("with stub $filestub length".mb_strlen($text));
    $Context->ZipSendString($filestub, $text);
}
else
{
    $a = array();
    foreach($rows as $page)
    {
        $text = "";
        if($include_proofers && isset($page['proofers']))
        {
            $pagename = $page['pagename'].".txt";
            $proofers = $page['proofers'];
            $hdrrow = "-----File: {$pagename}---\\$proofers\\";
            $hdrrow = str_pad($hdrrow, 74, "-", STR_PAD_RIGHT);
            $text .= "$hdrrow\n";
        }
        $text .= ($page['text'] == ""
                    ? _("not proofed")
                    : $page['text']);
        $a[$pagename] = $text;
        // say("save to file dir: ".$dir." filename ".$name);
        // say("topath: $topath");
        // file_put_contents($topath, $text);
    }
    // say("stub 2: ".$filestub);
    // say("zip and send files in: ".$dir." into Context->ZipSendDirectory");
    $Context->ZipSendArray($filestub, $a);
    // $Context->ZipSendDirectory($filestub, $dir);
}

exit;

function clean_up_temp($path)
{
    unlink($path);
    rmdir(dirname($path));
}

// vim: sw=4 ts=4 expandtab
?>
